package com.qf.dao.impl;


import com.qf.dao.GoodsDao;
import com.qf.pojo.PageBean;
import com.qf.pojo.TbGoods;
import com.qf.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Create by 周志豪 on 2022/8/20  10:29
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */

public class GoodsDaoImpl implements GoodsDao {

    @Override
    public List<TbGoods> findByType(Long typeId, PageBean pageBean) {
        //获取到连接
        QueryRunner runner = DbUtils.getRunner();
        //计算limit的偏移量
        int pyl = (pageBean.getCurrentPage() - 1) * pageBean.getPageSize();
        //我们要执行的sql
        String sql = "SELECT tg.*, tgz.name,tgz.pubdate,tgz.intro,tgz.picture,tgz.price,tgz.star,tgz.typeid  from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=? limit ?,?";
        //查询总条数的sql
        String countSql = "select COUNT(0) totalSize from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=?";
        List<TbGoods> goodsList = null;
        try {
            //指定sql获取到返回的结果
            goodsList = runner.query(sql, new BeanListHandler<>(TbGoods.class), typeId, pyl, pageBean.getPageSize());
            //获取到当前分类的商品的数量
            Map<String, Object> resultMap = runner.query(countSql, new MapHandler(), typeId);
            //设置当前查询到的集合数据
            pageBean.setList(goodsList);
            //设置总条数, 总条数就是我们第二个sql的返回结果,通过totalSize作为key保存在map中
            pageBean.setTotalSize((Long) resultMap.get("totalSize"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    @Override
    public TbGoods findById(Long id) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "SELECT tg.*, tgz.name,tgz.pubdate,tgz.intro,tgz.picture,tgz.price,tgz.star,tgz.typeid  from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tg.id=?";
        TbGoods goods = null;
        try {
            goods = runner.query(sql, new BeanHandler<>(TbGoods.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goods;
    }

    @Override
    public List<TbGoods> findByIds(List ids) {
        StringBuffer stringBuffer = new StringBuffer();
        //根据条件的个数拼接?
        for (int i = 0; i < ids.size(); i++) {
            stringBuffer.append("?");
            if (i != ids.size() - 1) {
                //只要不是最后一个条件,就拼接 ,
                stringBuffer.append(",");
            }
        }
        QueryRunner runner = DbUtils.getRunner();
        String sql = "SELECT tg.*, tgz.name,tgz.pubdate,tgz.intro,tgz.picture,tgz.price,tgz.star,tgz.typeid  from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tg.id in("+stringBuffer.toString()+")";
        List<TbGoods> goodsList = null;
        try {
            goodsList = runner.query(sql, new BeanListHandler<>(TbGoods.class),ids.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }
}
